#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <cstdlib>
#include <string.h>
#include <unistd.h>
#include "mt_proc.h"

#define MAX_THREADS 2

static int shutdown_g = 0;

#define TEST_RAND_MAX 1000

void do_work(int val)
{
	int count=0;
	int randnum =0; 
	MT_LOG_START(FUNC1,val);
	randnum= rand() % TEST_RAND_MAX;
	while (count!=randnum)count++;
	MT_LOG_END(FUNC1, val);
}

void thread_func(int val)
{
	while(!shutdown_g)
	{
		do_work(val);
	}
}

void* thread_proc(void *arg)
{
	int val= (int)arg;

	printf("Inside thread %d\n", val);

	thread_func(val);

	printf("Inside thread %d : Exiting\n", val);
}


int main()
{
	struct timespec now;	
	long long val;
	int count=MAX_THREADS;
	int i;
	pthread_t threads[MAX_THREADS];

   	MT_LOG_INIT();

	for (i=0;i<count;i++)
	{
		pthread_create(&threads[i], NULL, thread_proc, (void*)i);
	}
	printf("Going to sleep : Main thread\n");
	sleep(5);
	printf(" Time to stop workers\n");
	shutdown_g=1;
	for (i=0;i<count;i++)
	{
		pthread_join(threads[i], NULL);
	}
	MT_LOG_DUMP();
}
